import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_trip/widget/banner_widget.dart';
import 'package:flutter_trip/widget/grid_nav_widget.dart';
import 'package:flutter_trip/widget/local_nav_widget.dart';
import 'package:flutter_trip/widget/sales_box_widget.dart';
import 'package:flutter_trip/widget/sub_nav_widget.dart';

import '../dao/home_dao.dart';
import '../model/common_model.dart';
import '../model/grid_nav_model.dart';
import '../model/home_model.dart';
import '../model/sales_box_model.dart';
import '../widget/loading_container.dart';

const APPBAR_SCROLL_OFFSET = 100;
const SEARCH_BAR_DEFAULT_TEXT = '网红打卡地 景点 酒店 美食';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  double appBarAlpha = 0;
  bool _loading = false;
  List<CommonModel> localNavList = [];
  List<CommonModel> bannerList = [];
  List<CommonModel> subNavList = [];
  GridNavModel? gridNavModel;
  SalesBoxModel? salesBoxModel;

  // final bannerList = [
  //   "https://www.devio.org/io/flutter_app/img/banner/100h10000000q7ght9352.jpg",
  //   "https://o.devio.org/images/fa/photo-1601513041797-a79a526a521e.webp",
  //   "https://o.devio.org/images/other/as-cover.png",
  //   "https://o.devio.org/images/other/rn-cover2.png",
  // ];

  @override
  void initState() {
    super.initState();
    _handleRefresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xfff2f2f2),
      body: LoadingContainer(
          cover: true,
          isLoading: _loading,
          child: Stack(
            children: <Widget>[
              MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: RefreshIndicator(
                    onRefresh: _handleRefresh,
                    child: NotificationListener(
                      onNotification: (scrollNotification) {
                        if (scrollNotification is ScrollUpdateNotification &&
                            scrollNotification.depth == 0) {
                          //滚动且是列表滚动的时候
                          _onScroll(scrollNotification.metrics.pixels);
                        }
                        return false;
                      },
                      child: _listView,
                    )),
              ),
              _appBar
            ],
          )),
    );
  }

  Future<Null> _handleRefresh() async {
    setState(() {
      _loading = true;
    });
    try {
      HomeModel model = await HomeDao.fetch();
      setState(() {
        localNavList = model.localNavList ?? [];
        subNavList = model.subNavList ?? [];
        gridNavModel = model.gridNav;
        salesBoxModel = model.salesBox;
        bannerList = model.bannerList ?? [];
        _loading = false;
      });
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  _onScroll(offset) {
    double alpha = offset / APPBAR_SCROLL_OFFSET;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
    // print(appBarAlpha);
  }

  Widget get _listView {
    return ListView(
      children: <Widget>[
        _banner,
        LocalNavWidget(localNavList: localNavList),
        if (gridNavModel!=null) GridNavWidget(gridNavModel: gridNavModel!),
        SubNavWidget(subNavList: subNavList),
        if (salesBoxModel!=null) SalesBoxWidget(salesBox: salesBoxModel!)
      ],
    );
  }

  Widget get _appBar {
    return Column(
      children: <Widget>[
        Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              //AppBar渐变遮罩背景
              colors: [Color(0x66000000), Colors.transparent],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Container(
            padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            height: 80.0,
            decoration: BoxDecoration(
              color: Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
            ),
          ),
        ),
        Container(
            height: appBarAlpha > 0.2 ? 0.5 : 0,
            decoration: const BoxDecoration(
                boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 0.5)]))
      ],
    );
  }

  Widget get _banner {
    return Column(
      children: [
        BannerWidget(bannerList: bannerList),
      ],
    );
  }
}
